package com.czy.owner.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.czy.owner.R;
import com.czy.owner.entity.GoodsListModel;
import com.czy.owner.utils.glide.GlideUtils;
import com.easyrecycleview.adapter.BaseViewHolder;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.text.DecimalFormat;

/**
 * Created by asus on 2017/5/16.
 */
public class GoodsListAdapter extends RecyclerArrayAdapter<GoodsListModel.GoodsPageBean.ListBean> {
    private Context context;

    public GoodsListAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {

        return new GoodsListHolder(parent);

    }

    @Override
    public void OnBindViewHolder(BaseViewHolder holder, int position) {
        super.OnBindViewHolder(holder, position);
    }


    class GoodsListHolder extends BaseViewHolder<GoodsListModel.GoodsPageBean.ListBean> {
        private ImageView ivGoodsImg;
        private TextView tvGoodsName;
        private TextView tvGoodsPrice;
        private TextView tvGoodsDescrtion;
        private TextView tvMemberPrice;//会员价

        public GoodsListHolder(ViewGroup parent) {
            super(parent, R.layout.item_goods_list_recycler);
            ivGoodsImg = $(R.id.iv_goods_img);
            tvGoodsName = $(R.id.tv_goods_name);
            tvGoodsPrice = $(R.id.tv_goods_price);
            tvGoodsDescrtion = $(R.id.tv_goods_descrtion);
            tvMemberPrice = $(R.id.tv_member_price);
        }

        @Override
        public void setData(GoodsListModel.GoodsPageBean.ListBean model) {
            super.setData(model);
            x.image().bind(ivGoodsImg,model.getGoodsLogo(), new ImageOptions.Builder()
                    .setImageScaleType(ImageView.ScaleType.FIT_XY)
                    .setFailureDrawableId(R.mipmap.icon_goods_list_default)
                    .setLoadingDrawableId(R.mipmap.icon_goods_list_default)
                    .build());
            tvGoodsName.setText(model.getGoodsName());
            tvGoodsPrice.setText("￥"+new DecimalFormat("######0.00").format(model.getPrice()));
            tvGoodsDescrtion.setText(context.getResources().getString(R.string.goods_descrtion,model.getSoldCount(),"件",model.getCommentCount()));
            if(!TextUtils.isEmpty(model.getStoreMemberPrice())){
                tvMemberPrice.setVisibility(View.VISIBLE);
                tvMemberPrice.setText("￥"+String.format("%.2f",Double.parseDouble(model.getStoreMemberPrice())));
            }else {
                tvMemberPrice.setVisibility(View.GONE);
            }

        }
    }


}

